﻿using System.Web.Mvc;
using System.Web.Security;
using Bids.Core.DomainObjects;
using System;
using Bids.Core.Common.Security;
using Bids.Service;
using Bids.Data;
using System.Text.RegularExpressions;

namespace Bids.Web
{
    public class AccountController : Controller
    {
        IAuthenticationService _iAuthSrv;
        IRepository<Login> _iRpoLogin;
        IRepository<User> _iRpoUser;
        public AccountController(IAuthenticationService iAuthSrv
            , IRepository<Login> iRpoLogin
            , IRepository<User> iRpoUser)
        {
            _iAuthSrv = iAuthSrv;
            _iRpoLogin = iRpoLogin;
            _iRpoUser = iRpoUser;
        }

        public ActionResult Register()
        {
            return View();
        }
        [HttpPost]
        public ActionResult Register(FormCollection form)
        {
            User user = new User
            {
                CreateTime = DateTime.Now,
                NickName = form["NickName"].Trim(),
                Email = form["Email"].Trim(),
                UserName = form["Email"].Trim(),
                Type = (int)UserType.设计师,
                Login = new Login 
                {
                    UserName = form["Email"].Trim(),
                    Password = form["Password"].Trim().ToMD5()
                }
            };
            if (_iAuthSrv.Register(user))
            {
                _iAuthSrv.SignIn(user, false);
                return Redirect("/designer");
            }
            TempData["msg"] = "注册失败，请重新尝试";
            return View();
        }

        public ActionResult Login()
        {
            return View();
        }
        [HttpPost]
        public ActionResult Login(FormCollection form)
        {
            string phone = form["phone"];
            string email = form["Email"];
            string password = form["Password"].ToMD5();
            if (!string.IsNullOrEmpty(phone))
            {
                var login = _iRpoLogin.Find(t => t.UserName == phone && t.Password == password);
                if (login != null)
                {
                    _iAuthSrv.SignIn(login.User, false);
                    if (login.User.UserType == UserType.雇主)
                        return Redirect("/employer");
                    else
                        return Redirect("/designer/Index");
                }
                else
                {
                    TempData["msg_e"] = "手机号码或密码错误";
                }
            }
            else if (!string.IsNullOrEmpty(email))
            {
                Regex r = new Regex(@"\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*");
                if (r.IsMatch(email))
                {
                    var login = _iRpoLogin.Find(t => t.UserName == email && t.Password == password);
                    if (login != null)
                    {
                        _iAuthSrv.SignIn(login.User, false);
                        if (login.User.UserType == UserType.雇主)
                            return Redirect("/employer");
                        else
                            return Redirect("/designer/Index");
                    }
                    else
                    {
                        TempData["msg_d"] = "电子邮箱或密码错误";
                    }
                }
                else
                {
                    var login = _iRpoLogin.Find(t => t.User.NickName == email && t.Password == password);
                    if (login != null)
                    {
                        _iAuthSrv.SignIn(login.User, false);
                        if (login.User.UserType == UserType.雇主)
                            return Redirect("/employer");
                        else
                            return Redirect("/designer/Index");
                    }
                    else
                    {
                        TempData["msg_d"] = "昵称或密码错误";
                    }
                }
                
            }
            return View();
        }

        public ActionResult Logout()
        {
            _iAuthSrv.SignOut();
            return Redirect("/");
        }

        public string checkname(string phone,string email,string nickname)
        {
            string result = "false";
            if (!string.IsNullOrEmpty(phone))
            { 
               if( !_iRpoUser.Exists(t=>t.UserName == phone))
                   result = "true";
               else
                   result = "手机号码已存在";
            }
            else if (!string.IsNullOrEmpty(email))
            {
                if (!_iRpoUser.Exists(t => t.UserName == email))
                    result = "true";
                else
                    result =  "电子邮箱已存在";
            }
            else if (!string.IsNullOrEmpty(nickname))
            {
                if (!_iRpoUser.Exists(t => t.NickName == nickname))
                    result = "true";
                else
                    result = "该昵称已存在，请换一个昵称";
            }
            else
            {
                result = "用户名已存在，请换一个用户名";
            }

            return result;
        }
    }
}